package com.xmg.dataStructure.tree.avl;

public class AVLTreeDemo {
    public static void main(String[] args) {
//        int[] arr = {4,3,6,5,7,8};
//        int[] arr = {10,12,8,9,7,6};
        int[] arr = {10,11,7,6,8,9};
        AVLTree tree = new AVLTree();
        for (int i = 0; i < arr.length; i++) {
            tree.add(new Node(arr[i]));
        }
        System.out.println("中序遍历");
        tree.infixOrder();

        //旋转之前的高度
        System.out.println("旋转之前");
        System.out.println("树的高度："+tree.getRoot().height()+"--左子树："+tree.getRoot().leftHeight()+"--右子树："+tree.getRoot().rightHeight());
        System.out.println("根节点："+tree.getRoot());
    }

}

class AVLTree{
    private Node root;
    public void add(Node node){
        if(root==null){
            root = node;
        }else{
            root.add(node);
        }
    }

    public Node getRoot() {
        return root;
    }

    public Node search(int value){
        if(root == null){
            return null;
        }else{
            return root.search(value);
        }
    }

    public Node searchParent(int value){
        if(root == null){
            return null;
        }else{
            return root.searchParent(value);
        }
    }
    //返回以node为根节点的最小节点的值
    //删除已node为根节点的最小节点
    public int delRightTreeMin(Node node){
        Node taget = node;
        while (taget.left !=null){
            taget = taget.left;
        }
        delNode(taget.value);
        return taget.value;
    }

    //删除节点
    public void delNode(int value){
        if(root==null){
            return;
        }else{
            //1、查找待删除的节点targetNode
            Node targetNode = search(value);
            if(targetNode==null){
                return;
            }
            //如果二叉树只有一个节点，并且还能找到targetNode
            if(root.left==null && root.right==null){
                root = null;
                return;
            }
            Node parent = searchParent(value);
            //要删除的节点是叶子节点
            if(targetNode.left==null && targetNode.right==null){
                if(parent.left != null && parent.left.value == value){
                    parent.left = null;
                }else if (parent.right != null && parent.right.value == value){
                    parent.right = null;
                }
            }else if(targetNode.left!=null && targetNode.right!=null){//有两个子节点
                int minVal = delRightTreeMin(targetNode.right);//待删除节点的右子树的最小值  也可以是左子树的最大值
                targetNode.value = minVal;
            }else{//只有一颗子树
                if(targetNode.left!=null){
                    if(parent==null) {
                        root = targetNode.left;
                    }else{
                        if (parent.left != null && parent.left.value == value) {
                            parent.left = targetNode.left;
                        } else {
                            parent.right = targetNode.left;
                        }
                    }
                }else{
                    if(parent==null){
                        root = targetNode.right;
                    }else {
                        if (parent.left != null && parent.left.value == value) {
                            parent.left = targetNode.right;
                        } else {
                            parent.right = targetNode.right;
                        }
                    }
                }
            }
        }
    }

    public void infixOrder(){
        if(root==null){
            System.out.println("二叉排序树为空，不能遍历");
        }else{
            root.infixOrder();
        }
    }
}

//创建节点
class Node{
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }
    //返回左子树的高度
    public int leftHeight(){
        if(left==null){
            return 0;
        }
        return left.height();
    }
    //返回右子树的高度
    public int rightHeight(){
        if(right==null){
            return 0;
        }
        return right.height();
    }
    //返回该节点为根节点的树的高度
    public int height(){
        return Math.max(left==null?0:left.height(),right==null?0:right.height())+1;
    }

    private void leftRoate(){
        //创建新的节点，以当前根节点的值
        Node newNode = new Node(value);
        //把新的节点的左子树设置成当前节点的左子树
        newNode.left = left;
        //把新节点的右子树设置为当前节点的右子树的左子树
        newNode.right = right.left;
        //当前节点的值替换为右子节点的值
        value = right.value;
        //当前节点的右子树设置成右子树的右子树
        right = right.right;
        //当前节点的左子树设置成新节点
        left = newNode;
    }

    private void rightRoate(){
        Node newNode = new Node(value);
        newNode.right = right;
        newNode.left = left.right;
        value = left.value;
        left = left.left;
        right = newNode;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    public void add(Node node){
        if(node==null){
            return;
        }

        if(node.value<this.value){//向左遍历添加
            if(this.left==null){
                this.left = node;
            }else{
                this.left.add(node);
            }
        }else{//向右遍历添加
            if(this.right==null){
                this.right = node;
            }else{
                this.right.add(node);
            }
        }

        //当添加完一个节点后，如果右子树的高度-左子树的高度>1，坐旋转
        if(rightHeight()-leftHeight()>1){
            if(right!=null && right.leftHeight()>right.rightHeight()){
                right.rightRoate();
            }
            leftRoate();
        }else if(leftHeight()-rightHeight()>1){
            if(left!=null && left.rightHeight()>left.leftHeight()){
                left.leftRoate();
            }
            rightRoate();
        }
    }

    public void infixOrder(){
        if(this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right!=null){
            this.right.infixOrder();
        }
    }

    /**
     * 查找要删除的节点
     * @param value
     * @return
     */
    public Node search(int value){
        if(this.value == value){
            return this;
        }else if(value < this.value){
            if(this.left==null){
                return null;
            }
            return this.left.search(value);
        }else{
            if(this.right==null){
                return null;
            }
            return this.right.search(value);
        }
    }
    //查找要删除节点的父节点 如果没有返回null
    public Node searchParent(int value){
        if((this.left!=null && this.left.value == value) || (this.right != null && this.right.value == value)){
            return this;
        }else{
            if(value < this.value && this.left !=null){
                return this.left.searchParent(value);
            }else if(value >= this.value && this.right != null){
                return this.right.searchParent(value);
            }else{
                return null;
            }
        }
    }
}